
package com.yushu.common.util.bean;

import com.alibaba.fastjson.util.DeserializeBeanInfo;
import com.alibaba.fastjson.util.FieldInfo;
import com.alibaba.fastjson.util.TypeUtils;

import java.lang.reflect.Type;
import java.util.List;


 /**
 * 子类 转父类
 * @author test
 * @CreateDate 2020-12-03 15:47:24
 */
public class Father2Son {
    public static <Father, Son extends Father> void Copy(Father fa, Son so) {
        try {
            Class bc = fa.getClass();
            if (fa == null || so == null) {
                return;
            }
            DeserializeBeanInfo deserializeBeanInfo = DeserializeBeanInfo.computeSetters(so.getClass(), so.getClass());
            List<FieldInfo> getters = TypeUtils.computeGetters(fa.getClass(), null);

            List<FieldInfo> setters = deserializeBeanInfo.getFieldList();
            Object v;
            FieldInfo getterfield;
            FieldInfo setterfidld;

            for (int j = 0; j < getters.size(); j++) {

                getterfield = getters.get(j);
                for (int i = 0; i < setters.size(); i++) {
                    setterfidld = setters.get(i);
                    if (setterfidld.getName().compareTo(getterfield.getName()) == 0) {
                        v = getterfield.getMethod().invoke(fa);
                        setterfidld.getMethod().invoke(so, v);
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            ex.getStackTrace();
        }


    }


}